﻿using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Repository;
using CoreShop.Services;
using Sunny.UI;
using System;

namespace CoreShop.App
{
    public partial class frmDBConnect : UIForm
    {
        public frmDBConnect()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private bool SetConnect()
        {
            MySqlHelper.mysqlConnect = "";
            IniFiles.IniWriteValue("Connect", "server", txtserver.Text);
            IniFiles.IniWriteValue("Connect", "password", Security.Encrypt(txtpassword.Text));
            IniFiles.IniWriteValue("Connect", "uid", txtuid.Text);
            bool isDbConnect = false;
            ProcessUtil.SetWebUrlInit();
            if (isDbConnect)
            {
                UIMessageBox.Show("连接成功。", "提示：", UIStyle.Green, UIMessageBoxButtons.OK);
            }
            else
            {
                UIMessageBox.Show("连接失败，请检查。", "提示：", UIStyle.Green, UIMessageBoxButtons.OK);
            }
            return isDbConnect;
        }

        private void btnTry_Click(object sender, EventArgs e)
        {
            SetConnect();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (SetConnect())
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        private void frmDBConnect_Load(object sender, EventArgs e)
        {
            try
            {
                txtserver.Text = IniFiles.ReadIniData("Connect", "server");
                txtuid.Text = IniFiles.ReadIniData("Connect", "uid");
                string password = IniFiles.ReadIniData("Connect", "password");
                if (!string.IsNullOrEmpty(password))
                {
                    txtpassword.Text = Security.Decrypt(password);
                }
                else {
                    txtpassword.Text = "mysql1688";
                }
            }
            catch { }
        }

        private void btnDefault_Click(object sender, EventArgs e)
        {
            txtserver.Text = "127.0.0.1";
            txtuid.Text = "root";
            txtpassword.Text = "mysql1688";
        }
    }
}
